﻿#include "FastMoveDialog.h"
#include <QMessageBox>
#include <QDebug>
FastMoveDialog::FastMoveDialog(QWidget *parent) : QDialog(parent)
{
    initContent();
    initConnection();
}

void FastMoveDialog::initConnection()
{
    for(int i = 0; i < 10; i++)
    {
        connect(MoveToButton[i], SIGNAL(clicked(bool)), this, SLOT(on_moveTo()));
        connect(FetchPosButton[i], SIGNAL(clicked(bool)), this, SLOT(on_fetchPos()));
    }

}

void FastMoveDialog::setPos(int i, double x, double y)
{
    QString strX = XPosLabel[i]->text();
    QString strY = YPosLabel[i]->text();
    double xx = strX.toDouble();
    double yy = strY.toDouble();

    qDebug() << "xx = " << xx;
    qDebug() << "yy = " << yy;
    if(xx != 0 || yy != 0)
    {
        int ret = QMessageBox::warning(this, tr("JHPhoto"),
                                         u8"确定更改这个位置的坐标吗？\n",
                                         QMessageBox::Cancel | QMessageBox::Yes, QMessageBox::Cancel);
        if(ret == QMessageBox::Yes)
        {
            XPosLabel[i]->setNum(x);
            YPosLabel[i]->setNum(y);
        }

    }
    else
    {
        XPosLabel[i]->setNum(x);
        YPosLabel[i]->setNum(y);
    }
}

void FastMoveDialog::on_moveTo()
{
    QPushButton* button = static_cast<QPushButton *> (sender());

    for(int i = 0; i < 10; i++)
    {
        if( button == MoveToButton[i] )
        {
            QString strX = XPosLabel[i]->text();
            QString strY = YPosLabel[i]->text();
            double x = strX.toDouble();
            double y = strY.toDouble();

            emit gotoPos(x, y);
        }
    }
}

void FastMoveDialog::on_fetchPos()
{
    QPushButton* button = static_cast<QPushButton *> (sender());

    for(int i = 0; i < 10; i++)
    {
        if( button == FetchPosButton[i] )
        {
            emit fetchPos(i);
        }
    }
}

void FastMoveDialog::initContent()
{
    gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(1);

    for(int i = 0;i < 10; i++)
    {
        numLabel[i] = new QLabel(this);
        numLabel[i]->setNum(i);
        numLabel[i]->setFixedWidth(20);

        XPosLabel[i] = new QLabel(this);
        XPosLabel[i]->setNum(0);
        XPosLabel[i]->setFrameStyle(QFrame::Box | QFrame::Sunken);
        XPosLabel[i]->setMinimumWidth(60);
        //XPosLabel[i]->setFixedWidth(50);

        YPosLabel[i] = new QLabel(this);
        YPosLabel[i]->setNum(0);
        YPosLabel[i]->setFrameStyle(QFrame::Box | QFrame::Sunken);
        YPosLabel[i]->setMinimumWidth(60);
        //YPosLabel[i]->setFixedWidth(50);

        FetchPosButton[i] = new QPushButton(this);
        FetchPosButton[i]->setFixedWidth(50);
        //FetchPosButton[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        FetchPosButton[i]->setText("Fetch");

        MoveToButton[i] = new QPushButton(this);
        MoveToButton[i]->setFixedWidth(50);
        //MoveToButton[i]->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum);
        MoveToButton[i]->setText("Goto");

        gridLayout->addWidget(numLabel[i], i, 0, 1, 1);
        gridLayout->addWidget(XPosLabel[i], i, 1, 1, 1);
        gridLayout->addWidget(YPosLabel[i], i, 2, 1, 1);
        gridLayout->addWidget(FetchPosButton[i], i, 3, 1, 1);
        gridLayout->addWidget(MoveToButton[i], i, 4, 1, 1);
    }
}
